import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, App, AlertController } from 'ionic-angular';
import { ThemeProvider } from '../../providers/theme/theme';
import { LoginProvider } from '../../providers/login/login';
import { HttpErrorResponse } from '@angular/common/http';
import { LoginPage } from '../login/login';
import { NotificationProvider } from '../../providers/notification/notification';

/**
 * Generated class for the SettingPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-setting',
  templateUrl: 'setting.html',
})
export class SettingPage {
  selectedTheme: String;

  constructor(public navCtrl: NavController, 
    public navParams: NavParams, 
    private theme: ThemeProvider, 
    private api: LoginProvider,
    private app :App,
    private alertCtrl: AlertController,
    private notification: NotificationProvider
    ) {
    this.theme.getActiveTheme().subscribe(val => this.selectedTheme = val);
  }


  ionViewDidLoad() {
    console.log('ionViewDidLoad SettingPage');
  }

  changeTheme() {
    console.log('changeTheme: ' + this.selectedTheme);
    if (this.selectedTheme === 'dark-theme') {
      // 改变
      this.theme.setActiveTheme('light-theme');
      console.log('curTheme: ' + 'light-theme');
    } else {
      this.theme.setActiveTheme('dark-theme');
      console.log('curTheme: ' + 'dark-theme');
    }
  }

  /**
   * 退出登录
   */
  logout(){
    //提示确认
    this.notification.showConfirm('提示','是否退出？',res => {
      if(res==0) return;
      this.api.logout().subscribe(res => {
        if(res['success']){
          this.app.getRootNav().setRoot(LoginPage);
        }else{
        }
      },
        (err: HttpErrorResponse) => {
          if (err.error instanceof Error) {
            console.log("Client-side error occured.");
          } else {
            console.log("Server-side error occured.");
          }
        })
    })
    
  }

  //关于
  about(){
    const alert = this.alertCtrl.create({
      title: '',
      subTitle: '广州天智汇信息科技有限公司\n联系电话：020-37268789',
      buttons: ['OK']
    });
    alert.present();
  }

  showInfo(){
    //navCtrl页面跳转
    this.app.getRootNav().push('UserinfoPage');
  }
  
}
